const UserService = require('../services/UserService');
const UserController = {
    login: async (req, resp) => {
        const { account, password } = req.body;
        const loginResult = await UserService.login(account, password);
        if (loginResult.length === 0) {
            resp.send({ ok: 0 });
        } else {
            req.session.user = loginResult[0].nickname;
            req.session.uid = loginResult[0].id;
            resp.send({ ok: 1 });
        }
    },
    register: async (req, resp) => {
        const { account, nickname, password } = req.body;
        const registerValidate = await UserService.registerValidate(account);
        console.log('注册验证', registerValidate.length);
        if (registerValidate.length === 1) {
            resp.send({ ok: 2 });
        } else {
            const registerResult = await UserService.register(account, password, nickname);
            console.log('这是注册结果', registerResult.length);
            if (registerResult === 1) {
                resp.send({ ok: 1 });
            } else {
                resp.send({ ok: 0 });
            }
        }
    },
    logout: (req, resp) => {
        req.session.user = null;
        req.session.uid = null;
        resp.send({ ok: 1 });
    }
}
module.exports = UserController